﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SWC = System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetPerformanceDashboardTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetPageLifeExpectancyTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetConnectionsTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.CPUPercentageTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetAVGCountersTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetTopDatabasesTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetDiskReadsWritesPerSecTableAdapters;
using System.Configuration;
using SQLServerOptimizer.PL.WPF.Datasets;
using System.Windows.Forms;
using AG.Utilities;
using SQLServerHealthCheck.Model;
using SQLServerHealthCheck.SQLServer;
using System.Windows.Forms.Integration;

namespace SQLServerOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for IssuesReportControl.xaml
    /// </summary>
    public partial class PerformanceDashboardControl : SWC.UserControl
    {
        public PerformanceDashboardControl()
        {

            InitializeComponent();

        }


        // Global Paremeters for the Issue report ServiceId, Service, severityId, database, databaseId.

        public HealthCheckDataContext dc;


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (MainWindow.IsDatabaseCreated())
            {

            }
        }




        public void RefreshReport(Trace trace)
        {

            try
            {

                ReportViewer viewer = (ReportViewer)wfh_rptPerformanceDashboard.Child;

                PerformanceDashboardTableAdapter adapter = new PerformanceDashboardTableAdapter();
                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetPerformanceDashboard dataset = new DataSetPerformanceDashboard();
                adapter.Fill(dataset.PerformanceDashboard, trace.TraceID);


                BindingSource bindingSource = new BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "PerformanceDashboard";


                ReportDataSource reportDataSource = new ReportDataSource();
                viewer.Reset();
                reportDataSource.Name = "DataSetPerformanceDashboard";
                reportDataSource.Value = bindingSource;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\PerformanceDashboard.rdlc";
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = BorderStyle.None;

                viewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
                viewer.RefreshReport();



            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }





        }

        void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {

            int serverID = Convert.ToInt32(e.Parameters["ServerID"].Values[0]);
            int traceID = Convert.ToInt32(e.Parameters["TraceID"].Values[0]);
            if (e.DataSourceNames[0] == "DataSetDiskReadsWritesPerSec")
            {


                DiskReadsWritesPerSecTableAdapter adapter = new DiskReadsWritesPerSecTableAdapter();

                DataSetDiskReadsWritesPerSec dataset = new DataSetDiskReadsWritesPerSec();

                adapter.Fill(dataset.DiskReadsWritesPerSec, serverID, traceID);

                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "DiskReadsWritesPerSec";
                ReportDataSource reportDataSource = new ReportDataSource();

                reportDataSource.Name = "DataSetDiskReadsWritesPerSec";
                reportDataSource.Value = bindingSource;
                e.DataSources.Add(reportDataSource);
            }
            else
                if (e.DataSourceNames[0] == "CPUPercentage")
                {


                    CPUPercentageTableAdapter adapter = new CPUPercentageTableAdapter();

                    CPUPercentage dataset = new CPUPercentage();

                    adapter.Fill(dataset.CPUPercentageTable, traceID, serverID);
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = dataset;
                    bindingSource.DataMember = "CPUPercentageTable";
                    ReportDataSource reportDataSource = new ReportDataSource();

                    reportDataSource.Name = "CPUPercentage";
                    reportDataSource.Value = bindingSource;
                    e.DataSources.Add(reportDataSource);
                }
                else
                    if (e.DataSourceNames[0] == "DataSetPageLifeExpectancy")
                    {


                        PageLifeExpectancyTableAdapter adapter = new PageLifeExpectancyTableAdapter();

                        DataSetPageLifeExpectancy dataset = new DataSetPageLifeExpectancy();

                        adapter.Fill(dataset.PageLifeExpectancy, traceID, serverID);
                        BindingSource bindingSource = new BindingSource();
                        bindingSource.DataSource = dataset;
                        bindingSource.DataMember = "PageLifeExpectancy";
                        ReportDataSource reportDataSource = new ReportDataSource();

                        reportDataSource.Name = "DataSetPageLifeExpectancy";
                        reportDataSource.Value = bindingSource;
                        e.DataSources.Add(reportDataSource);
                    }
                    else if (e.DataSourceNames[0] == "DataSetConnections")
                    {


                        ConnectionsTableAdapter adapter = new ConnectionsTableAdapter();

                        DataSetConnections dataset = new DataSetConnections();

                        adapter.Fill(dataset.Connections, traceID, serverID);
                        BindingSource bindingSource = new BindingSource();
                        bindingSource.DataSource = dataset;
                        bindingSource.DataMember = "Connections";
                        ReportDataSource reportDataSource = new ReportDataSource();

                        reportDataSource.Name = "DataSetConnections";
                        reportDataSource.Value = bindingSource;
                        e.DataSources.Add(reportDataSource);
                    }
                    else if (e.DataSourceNames[0] == "DataSetAVGCounters")
                    {


                        AVGCountersAdapter adapter = new AVGCountersAdapter();

                        DataSetAVGCounters dataset = new DataSetAVGCounters();

                        adapter.Fill(dataset.AVGCounters, serverID, traceID);
                        BindingSource bindingSource = new BindingSource();
                        bindingSource.DataSource = dataset;
                        bindingSource.DataMember = "AVGCounters";
                        ReportDataSource reportDataSource = new ReportDataSource();

                        reportDataSource.Name = "DataSetAVGCounters";
                        reportDataSource.Value = bindingSource;
                        e.DataSources.Add(reportDataSource);
                    }
                    else if (e.DataSourceNames[0] == "DataSetTopDatabases")
                    {


                        TopDatabasesTableAdapter adapter = new TopDatabasesTableAdapter();

                        DataSetTopDatabases dataset = new DataSetTopDatabases();

                        adapter.Fill(dataset.TopDatabases, serverID, traceID);
                        BindingSource bindingSource = new BindingSource();
                        bindingSource.DataSource = dataset;
                        bindingSource.DataMember = "TopDatabases";
                        ReportDataSource reportDataSource = new ReportDataSource();

                        reportDataSource.Name = "DataSetTopDatabases";
                        reportDataSource.Value = bindingSource;
                        e.DataSources.Add(reportDataSource);
                    }



        }


    }
}
